package com.yphone.ui.ysj.view;

import android.content.Context;
import android.support.annotation.Nullable;
import android.support.v4.view.ViewPager;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;

import com.yphone.tools.NiceUtil;
import com.yw.game.floatmenu.Utils;
import com.zzkj.yunphone.R;
import com.iiordanov.bVNC.event.bVNCEvent;
import com.yphone.tools.MyLog;
import com.yphone.ui.login.manager.LoginManager;
import com.yphone.ui.login.respone.UserInfo;
import com.yphone.ui.ysj.adapter.*;
import com.yphone.ui.ysj.bean.RemoteConnectBean;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;

import java.util.ArrayList;
import java.util.List;

public class YSJCareView extends LinearLayout implements View.OnClickListener {

    private final Context mContext;
    private ViewPageCardAdapter viewPageCardAdapter;
    private ViewPager mViewCard;

    public YSJCareView(Context context) {
        super(context);
        this.mContext = context;
        intitview();
        initListener();
    }


    public YSJCareView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        this.mContext = context;
        intitview();
        initListener();
    }

    public YSJCareView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        this.mContext = context;
        intitview();
        initListener();
    }

    private void intitview() {
        EventBus.getDefault().register(this);
        LayoutInflater.from(getContext()).inflate(R.layout.view_ysj_careview, this);
        List<Object> objects = new ArrayList<>();
        objects.add(new Object());
        LoginManager instance = LoginManager.getInstance();
        UserInfo info = instance.getInfo();
        if (info != null) {
            List<RemoteConnectBean> remoteConnectBeans = info.remoteConnectBeans;
            for (int i = 0; i < remoteConnectBeans.size(); i++) {
                RemoteConnectBean remoteConnectBean = remoteConnectBeans.get(i);
                objects.add(remoteConnectBean);
            }
        }
        viewPageCardAdapter = new ViewPageCardAdapter(this.mContext, objects);
        this.mViewCard = (ViewPager) findViewById(R.id.vp_card);
        this.mViewCard.setAdapter(viewPageCardAdapter);
        this.mViewCard.setPageMargin(0);
        this.mViewCard.setClipChildren(false);
//        this.mViewCard.setPageTransformer(true, new CardTransformer());
        this.mViewCard.setOffscreenPageLimit(10);
        mViewCard.setPageMargin(NiceUtil.dp2px(mContext,-30));

        ViewGroup.LayoutParams layoutParams = mViewCard.getLayoutParams();
        layoutParams.width = NiceUtil.getScreenWidth(mContext) - NiceUtil.dp2px(mContext, 80);
        mViewCard.setLayoutParams(layoutParams);
//        this.mViewCard.setCurrentItem(1);

        viewPageCardAdapter.notifyDataSetChanged();
    }

    private void initListener() {

    }

    @Override
    public void onClick(View v) {

    }

    @Subscribe
    public void onEventMainThread(bVNCEvent.UInotifyEvent event) {
        MyLog.e("eventbus msg handle logout = "+event.isLogout);
        if (event.isLogout){
            viewPageCardAdapter = new ViewPageCardAdapter(this.mContext, null);
            mViewCard.setAdapter(viewPageCardAdapter);
            return;
        }
        List<Object> objects = new ArrayList<>();
        objects.add(new Object());
        LoginManager instance = LoginManager.getInstance();
        UserInfo info = instance.getInfo();
        if (info != null) {
            List<RemoteConnectBean> remoteConnectBeans = info.remoteConnectBeans;
            for (int i = 0; i < remoteConnectBeans.size(); i++) {
                RemoteConnectBean remoteConnectBean = remoteConnectBeans.get(i);
                objects.add(remoteConnectBean);
            }
        }
        MyLog.e("size = "+objects.size());
        viewPageCardAdapter.notifyView(objects);


    }

}
